package com.rt.easyjava.common.config.dozer;

import org.dozer.Mapper;
import org.dozer.MappingException;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by wxy on 2017/9/14.
 */
@Configuration
public class MyMapper {

    @Resource
    private Mapper mapper;


    /*public List<T2> mapList(List<T1> list, Class<T2> clazz){
        List<T2> returnList = new ArrayList<>();
        for(T1 bean:list){
            if(bean==null)continue;
            T2 dto = mapper.map(bean,clazz);
            returnList.add(dto);
        }

        return returnList;
    }*/


    public <T> List<T> maplist(Object source, Class<T> destinationClass) throws MappingException {
        List<T> returnList = new ArrayList<>();
        for (Object bean : (List) source) {
            if (bean == null) continue;
            T dto = mapper.map(bean, destinationClass);
            returnList.add(dto);
        }
        return returnList;
    }

    public <T> T map(Object source, Class<T> destinationClass) throws MappingException {
        return mapper.map(source, destinationClass);
    }

    public void map(Object source, Object destination) throws MappingException {
        mapper.map(source, destination);
    }

    public <T> T map(Object source, Class<T> destinationClass, String mapId) throws MappingException {
        return mapper.map(source, destinationClass, mapId);
    }

    public void map(Object source, Object destination, String mapId) throws MappingException {
        mapper.map(source, destination, mapId);
    }
}
